<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>脚本管理</title>
    <#include "/pubHeadStyle.html">
</head>
<body>
<div class="tablesize roles" style="padding:10px">
    <table id="scriptItem" lay-filter="scriptItem"></table>
</div>

<script type="text/html" id="headerToolsBar">
    <div class="ccpui-enterSel">
        <div class="layui-btn-container">
            <a class="layui-btn layui-btn-sm" lay-event="query">
                <i class="layui-icon layui-icon-search"></i>
                <span>查询</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                <span>新增</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="edit">
                <i class="layui-icon layui-icon-edit"></i>
                <span>编辑</span>
            </a>
            <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="delSome">
                <i class="layui-icon layui-icon-delete"></i>
                <span>删除</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="export">
                <i class="layui-icon layui-icon-export"></i>
                <span>导出</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="import">
                <i class="layui-icon layui-icon-download-circle"></i>
                <span>导入</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                <span>重置</span>
            </a>
        </div>
        <form class="layui-form" action="" lay-filter="scriptQueryForm" style="margin-top:10px">
            <div class="table-top-filter">
                <div class="table-filter-alone">
                    <label class="layui-form-label">名称</label>
                    <input type="text" name="Q_name_SL" id="neme" class="layui-input filter-value"
                           placeholder="名称"/>
                </div>
                <div class="table-filter-alone">
                    <label class="layui-form-label">脚本分类</label>
                    <select name="Q_category_S" id="category" class="filter-value">
                        <option value="">------全部------</option>
                    </select>
                </div>
            </div>
        </form>
    </div>
</script>
<script type="text/html" id="manageBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table', 'layer', 'util', 'commons', 'setter', 'totalFilter', 'common','admin','form'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var $ = layui.$;
        var setter = layui.setter;
        var commons = layui.commons;
        var common = layui.common;
        var totalFilter = layui.totalFilter;
        var admin = layui.admin;
        var form = layui.form;

        var tableH = commons.tabHeight();



        loadTable(serverPath.formPath + setter.formLinkPath + 'script/list/scriptList');

        //第一个实例
        function loadTable(url) {
            table.render({
                elem: '#scriptItem'
                , cellMinWidth: 80
                , title: '脚本管理'
                , height: tableH
                , url: url
                , method: 'post'
                ,defaultToolbar: ['filter', 'print']
                , toolbar: '#headerToolsBar'
                ,autoSort: false
                , page: true
                , cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'name', title: '名称', sort: true, align: 'center'}
                    , {field: 'category', title: '脚本分类', sort: true, align: 'center'}
                    , {fixed: 'right', title: '管理', toolbar: '#manageBar', width: 250, align: 'center'}
                ]]
                ,done:function () {
                    common.tableSelectCon('.ccpui-enterSel .layui-form input','a[lay-event=query]','.ccpui-enterSel');
                }
            });
            totalFilter.init('scriptItem');
        }
        loadCategory();
        function loadCategory(){
            var url = serverPath.formPath + setter.formLinkPath + 'script/queryCategoryList';
            $.ajax({
                url: url,
                type: "get",
                async:false,
                dataType: 'json',
                success: function (data) {
                    var returnData = data.data;
                    for(var i=0;i<returnData.length;i++){
                        var category = returnData[i];
                        $("#category").append("<option value=\""+category+"\">"+category+"</option>");
                    }

                }
            });
        }
        function addOrUpdate(type,data){
            layer.open({
                type: 2,
                title: type === 'add'?"新增":'编辑',
                shade: 0.3,
                area: ['800px', '630px'],
                content:  serverPath.formPath + setter.formLinkPath+ "script/view/scriptEdit"+(type === 'add'? '':'?id='+data),
                end: function () {
                    loadCategory();
                }
            })
        }

        // 监听行工具事件
        table.on('toolbar(scriptItem)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;//获取选中行数据
            switch (obj.event) {
                case 'query':
                    common.tableConditionRender("scriptItem", "scriptQueryForm", {where: {}}, false);
                    loadCategory();
                    break;
                case 'reset':
                    common.tableConditionRender("scriptItem", "scriptQueryForm", {where: {}}, true);
                    loadCategory();
                    break;
                case 'add':
                    addOrUpdate('add');
                    break;
                case 'edit':
                    if (data.length === 0){
                        layer.msg("还没有选择，请选择一项进行编辑！",{time: 2500, icon:5});
                        return false;
                    }
                    if (data.length !== 0 && data.length !== 1){
                        layer.msg("已经选择了多项，请选择一项进行编辑！",{time:2500,icon:5});
                        return false;
                    }
                    addOrUpdate('edit',data[0].id);
                    break;
                case 'delSome':
                    if(data.length === 0){
                        layer.msg("请选择记录!", {time: 2500, icon:5});
                        return false;
                    }
                    var ids = $.map(data,function(v){
                        return v.id;
                    });
                    layer.confirm('确定删除吗？',{skin: 'btn-class'}, function () {
                        $.ajax({
                            url: serverPath.formPath + setter.formLinkPath + 'script/del',
                            type:'post',
                            dataType: 'json',
                            data: {id: ids.toString()},
                            success: function (data) {
                                //提示消息
                                layer.msg(data.msg, {time: 2500, icon:6});
                                //执行重载
                                table.reload('scriptItem', {
                                    page: {
                                        curr: 1
                                    }
                                });
                                totalFilter.init('scriptItem');
                            }
                        })
                    });
                    break;
                case 'export':
                    if (data.length == 0) {
                        layer.confirm('还没有选择，是否导出全部？', {
                            btn: ['确定', '取消'] //按钮
                        }, function (index) {
                            layer.close(index);
                            var url = serverPath.formPath + setter.formLinkPath + 'script/export';
                            window.location.href = url;
                        });
                    } else {
                        layer.confirm('确定导出吗？', {
                            btn: ['确定', '取消'] //按钮
                        }, function (index) {
                            layer.close(index);
                            var ids = [];
                            for (var i = 0; i < data.length; i++) {
                                ids.push(data[i].id);
                            }
                            var tableIds = ids.join(',');
                            var url = serverPath.formPath + setter.formLinkPath + 'script/export?ids=' + tableIds;
                            window.location.href = url;
                        });
                    }
                    break;
                case 'import':
                    var iframeWin;
                    layer.open({
                        type: 2,
                        title: "导入",
                        shade: 0.3,
                        area: ['400px', '300px'],
                        content: serverPath.formPath +  setter.formLinkPath + "script/view/import",
                        success: function(layero, index){
                            iframeWin = window[layero.find('iframe')[0]['name']];
                        },
                        end:function () {
                            if(iframeWin.layui.needReload!=undefined){
                                table.reload('scriptItem', {
                                    page: {
                                        curr: 1
                                    }
                                });
                                totalFilter.init('scriptItem');
                            }
                        }
                    });
                    break;
            }
        });

        table.on('tool(scriptItem)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    layer.confirm('确定删除吗？',{skin: 'btn-class'}, function () {
                        $.ajax({
                            url: serverPath.formPath + setter.formLinkPath + 'script/del',
                            type:'post',
                            dataType: 'json',
                            data: {id:data.id},
                            success: function (data) {
                                //提示消息
                                layer.msg(data.msg, {time: 2500, icon:6});
                                //执行重载
                                table.reload('scriptItem', {
                                    page: {
                                        curr: 1
                                    }
                                });
                                totalFilter.init('scriptItem');
                            }
                        })
                    });
                    break;
                case 'edit':
                    addOrUpdate('edit',data.id);
                    break;
            }
        });

        common.tableSort("scriptItem",{});
    });
</script>
</body>
</html>